using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;

/// <summary>
/// 控制玩家移动,挂载到玩家身上
/// </summary>
public class PlayerControl : MonoBehaviour
{
/*************玩家移动相关**************/
    [Header("变量")]
    private Vector2 originalVelocity; //保存速度
    [Header("组件")]
    private Rigidbody2D rb;

    private PlayerInput playerInput;
    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    
    //
    private void Start()
    {
        //输入控制类激活
        playerInput = GetComponent<PlayerInput>();
    }
    
    //
    private void Update()
    {
        Shoot();
    }
    
    /// <summary>
    /// 发射子弹
    /// </summary>
    private void Shoot()
    {
        if (playerInput.isInputAttack)
        {
            AudioManager.Instance.PlayAudioSourceSFX(AudioSFXPath.PLAYER_ATTACK, this.gameObject);
            GameObject obj  = GenericsObjectPoolManager<GameObject>.Instance.CreateObject(PreformPath.PLAYER_BULLET);
            obj.SetActive(true);
            // 设置子弹位置
            obj.transform.position = new Vector3(this.gameObject.transform.position.x,this.gameObject.transform.position.y + 0.5f,this.gameObject.transform.position.z);
            Rigidbody2D rb = obj.gameObject.GetComponent<Rigidbody2D>();
            // 设置子弹发射 方向
            if (playerInput.axesX >= 0)
            {print("向右");
                // obj.transform.localScale = Vector3.one;
                rb.velocity = new Vector2(15f, 0);
            }
            else
            {print("向左");
                // obj.transform.localScale = new Vector3(-1, 1, 1);
                rb.velocity = new Vector2(-15f, 0);
            }
        }
    }

    //设置速度
    public void SetVelocity(Vector2 moveValue)
    {
        rb.velocity = moveValue;
    }

    //设置X轴速度
    public void SetVelocityX(float velocityX)
    {
        rb.velocity = new Vector2(velocityX * playerInput.axesX,rb.velocity.y);
    }

    //设置Y轴速度,状态机调用
    public void SetVelocityY(float velocityY)
    {
        rb.velocity = new Vector2(rb.velocity.x,velocityY);
    }

    /// <summary>
    /// Y轴锁定,由状态机调用
    /// 锁定false，解锁true
    /// </summary>
    /// <param name="value"></param>
    public void SetUseGravity(bool value)
    {
        originalVelocity = rb.velocity;
        if (value)
        {
            rb.constraints |= RigidbodyConstraints2D.FreezeRotation;
            rb.constraints &= ~RigidbodyConstraints2D.FreezePositionY;
            rb.velocity = new Vector2(rb.velocity.x, originalVelocity.y);
        }
        else
        {
            rb.constraints |= RigidbodyConstraints2D.FreezeRotation;
            rb.constraints |= RigidbodyConstraints2D.FreezePositionY;
        }
    }
    
    /// <summary>
    /// 滚动调用
    /// </summary>
    /// <param name="vector2">冲刺方向</param>
    /// <param name="repelDistance">冲刺距离</param>
    public void SetSprint(float sprintDistance)
    {
        rb.AddForce(new Vector2((int)playerInput.axesX, 0)*sprintDistance, ForceMode2D.Impulse);
    }
    
    /// <summary>
    /// 飞行调用
    /// </summary>
    /// <param name="flySpeed">飞行方向(含速度)</param>
    public void SetFly(Vector2 flySpeed)
    {
        rb.AddForce(flySpeed, ForceMode2D.Impulse);
    }
}